//
//  ViewController.swift
//  LSTools
//
//  Created by zrf on 06/29/2021.
//  Copyright (c) 2021 zrf. All rights reserved.
//

import UIKit
import LSTools

class ViewController: UIViewController {
    var smp =  DispatchSemaphore(value: 3)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        for i in 0 ... 10 {
            DispatchQueue.global().async { [unowned self] in
                LSLock.semaphoreLock(smp) {
                    Thread.sleep(forTimeInterval: 0.5)
                    print("打印内容:\(i)")
                }
            }
            DispatchQueue.global().async { [unowned self] in
                LSLock.semaphoreLock(self.smp) {
                    print("--内容数组\(i)")
                }
            }
        }
        print("roffa@qq.com".emailFormat())
        
        //MARK: LSConstant Demo
        print("App信息:" + appName() + appVersion() + "." + appBuildNum())
        print("屏幕宽\(lsScreenWidth)高\(lsScreenHeight)顶部安全区域高\(lsSafeTop)底部安全区域高\(lsSafeBottom)")
        let str = "0123456789abcdf"
        let data: Data! = str.data(using: .utf8)
        print(123)      //正常打印
        lsPrint(123)    //定制化打印，正式环境不打印，打印内容自动存储在本地，方便根据log追踪问题
        lsPrint(data.hexString) //Data数据以16进制输出   303132333435363738396162636466
        
        //MARK: LSPathManager Demo
        @LSPathProtected var path: String = "w3w/1/2/3"  //本地自动生成完整路径
        lsPrint(path)  ///Users/a123456/Library/Developer/CoreSimulator/Devices/85C4F321-37BA-4464-B64F-819A3631A8B4/data/Containers/Data/Application/39C04FA7-0641-4BA9-BE18-59243C94A392/Documents/Local/w3w/1/2/3
        @LSPathProtected var path1: String = "w4/1"
        @LSPathProtected var path2: String = "w1/1/2/3"
        @LSPathProtected var path3: String = "w4/1/2/3"
        LSPathManager.create(path3, data: "123qw写点东西".data(using: .utf8)!)  //根据文件路径，写入数据
        LSPathManager.copy("w4", to:"w5")  //w4文件夹copy到w5
        lsPrint("读取文件内容Data:"+LSPathManager.data(path3)!.string)
        LSPathManager.insert(path3, data: "\n\n换\n能不能顺利的插入到后面进入呀".data(using: .utf8)!)   //文件末尾插入  读取文件内容Data:123qw写点东西
        lsPrint("读取文件片段:" + LSPathManager.read(path3, from: 1, length: 10).string)//读取文件片段:23qw写点
        lsPrint("获取当前子路径列表:" + LSPathManager.getCurPaths("w2w").description) //获取当前子路径列表["1"]
        lsPrint("获取所有子路径列表:" + LSPathManager.getSubPaths(LSPathManager.prefixDocPath).description)//获取所有子路径列表["w3w", "w3w/1", "w3w/1/2", "w4", "w4/1", "w4/1/2", "w4w", "w4w/1", "w4w/1/2", "w1", "w1/1", "w1/1/2", "w2w", "w2w/1", "w2w/1/2"]
        lsPrint("获取文件夹下文件列表:" + LSPathManager.getSubFiles(LSPathManager.prefixDocPath).description)//获取文件夹下文件列表:["w4/1/2/3", "w5/1/2/3", "Log/log.txt"]
        print(LSPathManager.size("Log/log.txt"))  //15187
        
        lsPrint(self.classString())  //获取类名. 打印出：LSTools_Example.ViewController
        //MARK: 浮点数
        print("浮点数整型输出1.9994=\(1.9994.i), 浮点数为整数转换3.00=\(3.00.cleanZero)"  )  //浮点数整型输出1.9994=1, 浮点数为整数转换3.0017=3.0017
        
        //MARK: 国际化
        String.language = .cn  //  此行在本地改变语言后赋值，默认显示中文
        lsPrint("扫描大师".local)       //本地国际化
        
        //MARK: UIImage
        //更多操作请查看Image+Extension类
        let img = UIImage(named: "Image")
        print("图片大小\(img!.kb)kb")
        print("图片转jpegData=\(img!.compressedData(quality: 0.5)!.description)")
        print("图片转png Base64输出:\(img!.pngBase64String()!)")
        var newImg = img!.imageWithTintColor(.main) //图片颜色渲染为主色显示
        newImg = newImg.rotated(on: 90)  //旋转90°
        newImg.compress(toByte: 100*1024)  //压缩图片到100kb
        //MARK: String
        let testStr = "13854241489"
        print("是否是手机\(testStr.isPhone)，是否是数字\(testStr.isNumeric),是否是邮箱\(testStr.isEmail)")  //是否是手机true，是否是数字true,是否是邮箱false
        lsPrint("md5(\(str))=" + str.md5)  //MD5输出
        lsPrint("sha256(\(str))=" + str.sha256)  //sha256输出    sha256(0123456789abcdf)=6ef6e6a482b064c519c94d2157ef6b3c24ade3ed79b5155749f8fc13f0940d97
        lsPrint(str.base64 + " " + str.decodeBase64)  //base64输出  MDEyMzQ1Njc4OWFiY2Rm 0123456789abcdf
        try? testStr.dict()  //json str转字典
        print("随机字符串:" + String(randomOfLength: 8))  //
        testStr.copyToPasteboard()   //内容复制到剪切板
        
        //MARK: Date
        let date = Date()
        print("\(date.year)第\(date.weekOfYear)周, iso:\(date.iso8601String)")//2021第27周, iso:2021-06-30T05:56:49.011Z
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func createButtonWithImage(_ image: UIImage, _ title: String) -> UIButton {
        
        let button = UIButton(type: .system)
        button.setImage(image.original, for: .normal)
        button.setTitle(title, for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 10)
        button.setupButtonImageAndTitlePossitionWith(padding: 10, style: .left)
        button.frame.origin = CGPoint(x: 100, y: 100)
        button.size = CGSize(width: 200, height: 100)
        button.backgroundColor = .black
        return button
    }
}

